package com.geek.service.impl;


import com.geek.bean.data.CommentListData;
import com.geek.bean.vo.CommentVo;
import com.geek.mapper.MarketCommentMapper;
import com.geek.mapper.MarketGoodsMapper;
import com.geek.mapper.MarketUserMapper;
import com.geek.model.MarketComment;
import com.geek.model.MarketCommentExample;
import com.geek.model.MarketUser;
import com.geek.model.MarketUserExample;
import com.geek.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketUserMapper userMapper;

    @Override
    public Map<String, Object> findById(Integer valueId, Byte valueType, Integer page, Integer limit) {
        MarketCommentExample example = new MarketCommentExample();
        example.createCriteria()
                .andTypeEqualTo(valueType)
                .andValueIdEqualTo(valueId)
                .andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketComment> data = commentMapper.selectByExample(example);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(data);
        long count = pageInfo.getTotal();

        Map<String, Object> commentMap = new HashMap<>();
        commentMap.put("count", count);
        commentMap.put("data", data);

        return commentMap;
    }

    @Override
    public CommentVo count(Byte type, Integer valueId) {
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andValueIdEqualTo(valueId);
        criteria.andDeletedEqualTo(false);

        List<MarketComment> comments = commentMapper.selectByExample(example);
        int countSum = comments.size();
        int countHasPicture = 0;
        for (MarketComment comment : comments) {
            if (comment.getHasPicture())
                countHasPicture++;
        }
        CommentVo commentVo = new CommentVo();
        commentVo.setAllCount(countSum);
        commentVo.setHasPicCount(countHasPicture);
        return commentVo;
    }

    @Override
    public MarketComment post(int userId, Integer valueId, String content, short star, Boolean hasPicture, byte type, String[] picUrls) {
        MarketComment postCommentVo = new MarketComment();
        postCommentVo.setUserId(userId);
        postCommentVo.setAddTime(new Date());
        postCommentVo.setUpdateTime(new Date());
        postCommentVo.setType(type);
        postCommentVo.setValueId(valueId);
        postCommentVo.setContent(content);
        postCommentVo.setStar(star);
        postCommentVo.setHasPicture(hasPicture);
        postCommentVo.setPicUrls(picUrls);
        ;
        // 获取自增id
        commentMapper.insertSelective(postCommentVo);

        return postCommentVo;
    }

    @Override
    public List<CommentListData> list(Byte type, Short showType, Integer valueId, Integer page, Integer limit) {
        // 获取评论列表
        List<MarketComment> commentList = Commentlist(type, showType, valueId, page, limit);
        ;
        // 获取其中的用户id
        List<Integer> userIdList = commentList.stream()
                .map(MarketComment::getUserId)
                .distinct()
                .collect(Collectors.toList());
        // 获取 id 对应的信息
        Map<Integer, MarketUser> userId = UserId(userIdList);

        List<CommentListData> collect = commentList.stream().map(
                comment -> {
                    CommentListData commentListData = new CommentListData();
                    commentListData.setPicList(Arrays.toString(comment.getPicUrls()));
                    commentListData.setAdminContent(comment.getAdminContent());
                    commentListData.setContent(comment.getContent());
                    commentListData.setAddTime(comment.getAddTime());
                    if (comment.getUserId() != null) {
                        commentListData.setUserInfo(CommentListData.UserInfo.builder()
                                .nickName(userId.get(comment.getUserId()).getNickname())
                                .avatarUrl(userId.get(comment.getUserId()).getAvatar())
                                .build());
                        return commentListData;
                    } else {
                        commentListData.setUserInfo(CommentListData.UserInfo.builder()
                                .nickName("")
                                .avatarUrl("")
                                .build());
                        return commentListData;
                    }
                }).collect(Collectors.toList());
        return collect;
    }

    // 用户id对应的信息
    private Map<Integer, MarketUser> UserId(List<Integer> userIdList) {
        if (userIdList.isEmpty()) {
            return new HashMap<>();
        } else {

            MarketUserExample example = new MarketUserExample();
            MarketUserExample.Criteria criteria = example.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andIdIn(userIdList);
            List<MarketUser> userList = userMapper.selectByExample(example);

            Map<Integer, MarketUser> userIdMap = new HashMap<>();
            for (MarketUser user : userList) {
                userIdMap.put(user.getId(), user);
            }
            return userIdMap;
        }
    }

    // 获取评论列表
    private List<MarketComment> Commentlist(Byte type, Short showType, Integer valueId, Integer page, Integer limit) {
        MarketCommentExample example = new MarketCommentExample();
        example.setOrderByClause("add_time desc");
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        PageHelper.startPage(page, limit);
        if (showType == 1) {
            criteria.andHasPictureEqualTo(true);
        }
        return commentMapper.selectByExample(example);
    }
}

